package com.oneraku.service.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.oneraku.service.dao.MerchantCategoryMapper;
import com.oneraku.service.domain.Merchant;
import com.oneraku.service.dao.MerchantMapper;
import com.oneraku.service.domain.MerchantCategory;
import com.oneraku.service.service.IMerchantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oneraku.service.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 商户 服务实现类
 * </p>
 *
 * @author renhui <337375464@qq.com>
 * @since 2020-03-12
 */
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements IMerchantService {
    @Autowired
    private MerchantCategoryMapper categoryMapper;
    @Override
    public R hotMerchant() {
        List<Merchant> merchantList = list();
        Map<String,List<Merchant>> merchantMap = merchantList.stream().collect(Collectors.groupingBy(i -> i.getCategory()));
        return R.ok(merchantMap);
    }
    @Override
    public R hotMerchantV2() {
        Map<String,List<Merchant>> merchantMap = list().stream().collect(Collectors.groupingBy(i -> i.getCategory()));
        List<MerchantCategory> categories = categoryMapper.selectList(Wrappers.<MerchantCategory>lambdaQuery().orderByDesc(MerchantCategory::getSeq));
        List<JSONObject> ret = categories.stream().map(item -> {
            JSONObject obj = new JSONObject();
            obj.put("category",item.getCategory());
            List<Merchant> merchants = merchantMap.get(item.getCategory());
            if(null != merchants){
                obj.put("merchants",merchants);
            } else {
                obj.put("merchants", Collections.EMPTY_LIST);
            }
            return obj;
        }).collect(Collectors.toList());
        return R.ok(ret);
    }
}
